package com.qitianyong.selfclass;

import com.gkusbsdk.SZUSBRecordDate;
import com.gkusbsdk.SZUSBTime;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by qitianyong on 2017/3/3.
 */

public class PublicMethod {

    public static String getCurrentTime(){

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date curDate = new Date(System.currentTimeMillis());
        
        return formatter.format(curDate);
    }

    public static long getCurrentMillis(){
        return System.currentTimeMillis();
    }

    public static SZUSBTime CurrentTime2USBTime(){

        Calendar c = Calendar.getInstance();
        SZUSBTime time = new SZUSBTime();

        time.__year = c.get(Calendar.YEAR);
        time.__month = c.get(Calendar.MONTH)+1;
        time.__day = c.get(Calendar.DAY_OF_MONTH);
        time.__hour = c.get(Calendar.HOUR_OF_DAY);
        time.__minute = c.get(Calendar.MINUTE);
        time.__second = c.get(Calendar.SECOND);

        return time;
    }

    public static void sleep(int ms){

        try {
            Thread.sleep(ms);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    // 个位数前方加0变成2位
    public static String dataTo2Char(int data){
        if(data >=0 && data<10){
            return "0"+data;
        }

        return ""+data;
    }

    // 字符串解析成Calendar
    public static SZUSBRecordDate parseString2RecordDate(String filename)  {
        int index  = filename.lastIndexOf(".");
        if(index < 0){
            return null;
        }

        LogUtils.i("file name", "name:"+filename);
        String name = filename.substring(0, index);
        SimpleDateFormat fd = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = null;

        try {
            date = fd.parse(name);
            if(date == null){
                return null;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar calandar = Calendar.getInstance();
        calandar.setTime(date);

        SZUSBRecordDate rddate = new SZUSBRecordDate();

        rddate.__year = calandar.get(Calendar.YEAR);
        rddate.__month = calandar.get(Calendar.MONTH)+1;
        rddate.__day = calandar.get(Calendar.DAY_OF_MONTH);
        rddate.__hour = calandar.get(Calendar.HOUR_OF_DAY);
        rddate.__minute = calandar.get(Calendar.MINUTE);
        rddate.__second = calandar.get(Calendar.SECOND);

        LogUtils.i("date", ""+calandar.get(Calendar.YEAR)+"-"+
                (calandar.get(Calendar.MONTH)+1)+"-"+calandar.get(Calendar.DAY_OF_MONTH)+
                " "+calandar.get(Calendar.HOUR_OF_DAY)+":"+calandar.get(Calendar.MINUTE)+":"+
                calandar.get(Calendar.SECOND));

        return rddate;
    }
}
